﻿using NQQOpenApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DemoSite.usercenter
{
    public partial class qqlogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string state = Request.QueryString["state"];
            string code = Request.QueryString["code"];

            if (state != Session["QQState"].ToString())
            {
                Response.Write("状态验证不合法。");
                Response.End();
                return;
            }

            if (string.IsNullOrEmpty(code))
            {
                Response.Write("验证失败");
                Response.End();
                return;
            }

            var manager = new PCApiManager("你的AppID", "你的AppKey");
            var tocken = manager.GetAccessToken(code, "你的回调地址");
            if (tocken.Error == null || tocken.Error.ErrorCode == "0")
            {
                var openid = manager.GetOpenID(tocken.AccessToken);

                if (openid.Error == null || openid.Error.ErrorCode == "0")
                {
                    var userinfo = manager.GetUserInfo(tocken.AccessToken, openid.OpenID);
                    if (userinfo.Ret == "0")
                    {
                        ImageUserPic.ImageUrl = userinfo.Figureurl_qq_2;
                        lblUserNick.Text = userinfo.NickName;
                        lblAccessToken.Text = tocken.AccessToken;
                        lblOpenId.Text = openid.OpenID;
                    }
                }
            }
        }
    }
}